//
//  UnlockingModelViewController.m
//  RONGAO
//
//  Created by Kendama on 2018/2/26.
//  Copyright © 2018年 wei wang. All rights reserved.
//

#import "UnlockingModelViewController.h"

@interface UnlockingModelViewController ()<UITableViewDelegate,UITableViewDataSource>

/** UItableView **/
@property(nonatomic,strong) UITableView *tableView;

@property(nonatomic,strong) NSArray *dataSoure;
@property(nonatomic,assign) NSInteger index;

@end

@implementation UnlockingModelViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeLockModelCallBack:) name:AddFingerprintOrCardStateNotification object:nil];
}


- (void)changeLockModelCallBack:(NSNotification *)notice {
    [SVProgressHUD dismiss];
    NSString * str = notice.object;
    if ([str intValue] == 1) {
        [self.navigationController popViewControllerAnimated:YES];
    } else {
        [SVProgressHUD showErrorWithStatus:@"设置失败"];
    }
}

#pragma mark - --------------------------点击事件--------------------------
- (void)onRightButtonClick:(id)sender
{
    if (self.deviceMac.length == 0) {
        return;
    }
    [SVProgressHUD showWithStatus:nil];
    [SVProgressHUD dismissWithDelay:5];
    [[WiFiSendDataManager defaultService] unlockingModelSetting:self.deviceMac value:[NSString stringWithFormat:@"%ld",self.index+1]];
}

#pragma mark - --------------------------UITableView dataSource--------------------------

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSoure.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 56;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return CGFLOAT_MIN;
    
}

#pragma mark - --------------------------UITableView delegate--------------------------

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"UnlockingModel"];
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    cell.detailTextLabel.font = [UIFont systemFontOfSize:14];
    cell.detailTextLabel.textColor = [UIColor lightGrayColor];
    
    NSArray *array = self.dataSoure[indexPath.row];
    cell.textLabel.text = array.firstObject;
    cell.detailTextLabel.text = array.lastObject;

    if ( indexPath.row == self.index )
    {
        cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"video_set_bg_sel"]];
    }
    else
    {
        cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"video_set_bg_nor"]];
    }
    
    return cell;
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    self.index = indexPath.row;
    [tableView reloadData];
}




- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return YES;
}


#pragma mark - --------------------------懒加载--------------------------
- (UITableView *)tableView{
    
    if (!_tableView) {
        
        __weak UIView *superView = self.view;
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        [superView addSubview:_tableView];
        _tableView.scrollEnabled = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
    }
    return _tableView;
}

- (NSArray *)dataSoure{
    
    if (!_dataSoure) {
        //_dataSoure = @[@[@"常开模式",@"门锁保持常开"],@[@"单开模式",@"使用指纹/密码/卡其中一种开门"],@[@"双指纹模式",@"使用两组指纹开门"],@[@"指纹密码模式",@"使用指纹和密码开门"],@[@"指纹卡片模式",@"使用指纹和卡片开门"]];
        _dataSoure = @[@[@"单开模式",@"使用指纹/密码/卡其中一种开门"],@[@"指纹密码模式",@"使用指纹和密码开门"],@[@"指纹卡片模式",@"使用指纹和卡片开门"]];
    }
    return _dataSoure;
}


#pragma mark - --------------------------约束--------------------------
- (void)masLayoutSubview{
    
    __weak UIView *superView = self.view;
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(superView);
    }];
}

#pragma mark - --------------------------导航栏--------------------------
- (void)setNavigationStyle
{
    [self setLeftButtonImage:[UIImage imageNamed:@"icon_back_nor"]];
    [self setRightButtonTitle:@"保存"];
    [self setNavigationTitle:@"门锁设置" titleColor:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
